#include <stdio.h>
#include <string.h>
#include "system_config.h"
#include "system_types.h"
#include "device.h"
#include "debug_assist.h"
#include "event.h"
#include "soft_timer.h"
#include "util.h"
#include "uart.h"
#include "queue.h"


//OLD FORMS
//#define debug_assist_dev_read(buffer, size)		uart_receive_array(UART1, buffer, size)
//#define debug_assist_dev_write(buffer, size)	uart_send_array(UART1, buffer, size)

//NEW FORMS
#if DEBUG_ASSISTANT_PORT == 0
#define debug_assist_dev_read(buffer, size)		uart0_read(buffer, size)
#define debug_assist_dev_write(buffer, size)	uart0_write(buffer, size)
#elif DEBUG_ASSISTANT_PORT == 1
#define debug_assist_dev_read(buffer, size)		uart1_read(buffer, size)
#define debug_assist_dev_write(buffer, size)	uart1_write(buffer, size)
#elif DEBUG_ASSISTANT_PORT == 2
#define debug_assist_dev_read(buffer, size)		uart2_read(buffer, size)
#define debug_assist_dev_write(buffer, size)	uart2_write(buffer, size)
#elif DEBUG_ASSISTANT_PORT == 3
#define debug_assist_dev_read(buffer, size)		uart3_read(buffer, size)
#define debug_assist_dev_write(buffer, size)	uart3_write(buffer, size)
#else
#define debug_assist_dev_read(buffer, size)		0
#define debug_assist_dev_write(buffer, size)	
#endif

#define READ_BUFFER_SIZE						32


//uart transciver
static unsigned char  debug_assist_buffer[READ_BUFFER_SIZE];
static unsigned char  debug_assist_buffer_length = 0;

int	 debug_printf(const char * fmt, ...)
{
	debug_assist_dev_write(fmt, strlen(fmt));
	return 0;
}

static bool degug_default_command(unsigned char * cmd, unsigned char length)
{
	if(strncmp(cmd, "$TEST\r\n", min(7,length))==0)
	{
		debug_assist_dev_write("$OK\r\n", 5);
	}
	else if(strncmp(cmd, "$START\r\n", min(8,length))==0)
	{
		debug_assist_dev_write("$OK\r\n",5);
	}
	else if(strncmp(cmd, "$STOP\r\n", min(7,length))==0)
	{
		debug_assist_dev_write("$OK\r\n",5);
	}
	else if(strncmp(cmd, "$POWER\r\n", min(8,length))==0)
	{
		debug_assist_dev_write("$OK\r\n",5);
	}
	else if(strncmp(cmd, "$ECHO:\r\n", min(8,length))==0)
	{
		debug_assist_dev_write("$OK\r\n",5);
	}
	return TRUE;
}

static void debug_assist_process_read_content(function_with_buffer * fncommand)
{
	unsigned char i,shift;
	
	i = shift = 0;
	while(i<debug_assist_buffer_length)
	{
		while((i<debug_assist_buffer_length) && (debug_assist_buffer[i]!='$'))i++;
		if(i<debug_assist_buffer_length)//found '$'
		{
			shift = i++;
			while((i+1<debug_assist_buffer_length) && ((debug_assist_buffer[i]!='\r')||(debug_assist_buffer[i+1]!='\n')))i++;
			if(i+1<debug_assist_buffer_length)//found '\r\n'
			{
				i+=2;
				fncommand(debug_assist_buffer+shift, i-shift);
				shift = i;
			}
			else
			{
				if((shift==0)&&(i+1==READ_BUFFER_SIZE))//command is too long. buffers can't not store a complete command!
				{
						shift = READ_BUFFER_SIZE;
				}
				break;
			}
		}
		else
		{
			shift = i;
			break;
		}
	}

	for(i=0; i<debug_assist_buffer_length-shift; i++)
	{
		debug_assist_buffer[i] = debug_assist_buffer[i+shift];
	}
	debug_assist_buffer_length -= shift;
}

void debug_assist_pull(function_with_buffer * fncommand)
{
	unsigned char n;
	
	if(debug_assist_buffer_length < READ_BUFFER_SIZE)
	{
		n = READ_BUFFER_SIZE - debug_assist_buffer_length;
		n = debug_assist_dev_read(debug_assist_buffer + debug_assist_buffer_length, n);
		if(n>0)
		{
			debug_assist_buffer_length += n;
			debug_assist_process_read_content(fncommand);
			//debug_assist_dev_write(debug_assist_buffer, debug_assist_buffer_length);debug_assist_buffer_length = 0;
		} 
	}
}


